﻿using UnityEngine;
using System.Collections;

public class PlayerShooting : MonoBehaviour {

	public GameObject target;
	public GameObject[] bullet;
	public static float speed = 20f;
	public static float spread = 0f;
	public static float rate = 0.2f;
	public static bool automatic = false;
	public static float cost = 0.5f;
	private bool need_keyup = false;
	private bool need_cooldown = false;
	private float t = 0.2f;
	
	public PlayerHealth hp_system;
	
	public static int gun_level = 1;
	

	// Use this for initialization
	void Start () {
	
		if(hp_system == null)
			hp_system = transform.parent.transform.parent.GetComponent<PlayerHealth>();
			
		ApplyGunLevel();
			
	}
	void ApplyGunLevel()
	{
		switch(gun_level)
		{
			// single
		case 1:
			speed = 20f;
			spread = 0f;
			rate = 0.25f;
			automatic = true;
			need_cooldown = false;
			cost = 0.5f;
			break;
			
			// double
		case 2:
			speed = 25f;
			spread = 0f;
			rate = 0.2f;
			automatic = true;
			need_cooldown = false;
			cost = 0.7f;
			break;
			
			// triple
		case 3:
			speed = 30f;
			spread = 0f;
			rate = 0.15f;
			automatic = true;
			need_cooldown = true;
			cost = 0.9f;
			break;
			
			// yellow bouncy laser
		case 4:
			speed = 50f;
			spread = 0.1f;
			rate = 0.2f;
			automatic = true;
			need_cooldown = true;
			cost = 1.0f;
			break;
			
			// red
		case 5:
			speed = 30f;
			spread = 0.0f;
			rate = 0.05f;
			automatic = true;
			need_cooldown = true;
			cost = 0.5f;
			break;

			// flamethrower
		case 6:
			speed = 16f;
			spread = 0.4f;
			rate = 0.0025f;
			automatic = true;
			need_cooldown = true;
			cost = 0.125f;
			break;
			
			
		default:
				print ("Gun level "+gun_level+" not defined");
				gun_level = 3;
				speed = 30f;
				spread = 0f;
				rate = 0.15f;
				automatic = true;
				need_cooldown = true;
				cost = 0.9f;
			break;
		}
	}
	public void ResetGunLevel(int min)
	{
		//print ("ResetGunLevel disabled");
	
		if(gun_level >= min)
			gun_level = min;
		else
			gun_level = 1;
		
		ApplyGunLevel();
		
	}
	public void AddGunLevel()
	{
		gun_level++;
		//gun_level = Random.Range(1,7);
		
		if(gun_level>=10)
		{
			gun_level=10;
		}

		
		ApplyGunLevel();
	}

	
	void LookAtZ(GameObject ob, Vector3 target_pos)
	{
		Vector3 dir = target_pos-ob.transform.position;
		dir.Normalize();
		
		Quaternion target_q = ob.transform.rotation;
		
		if(dir.x > 0f && dir.x > Mathf.Abs(dir.y))// oikeelle
			target_q = Quaternion.LookRotation(target.transform.position - ob.transform.position,Vector3.down);
		if(dir.x < 0f && dir.x < -Mathf.Abs(dir.y))// vasemmalle
			target_q = Quaternion.LookRotation(target.transform.position - ob.transform.position,Vector3.up);
		
		if(dir.y > 0f && dir.y > Mathf.Abs(dir.x))// ylös
			target_q = Quaternion.LookRotation(target.transform.position - ob.transform.position,Vector3.right);
		if(dir.y < 0f && dir.y < -Mathf.Abs(dir.x))// alas
			target_q = Quaternion.LookRotation(target.transform.position - ob.transform.position,Vector3.left);
		
		target_q.x=0;
		target_q.y=0;
		
		ob.transform.rotation = target_q;
	}
	// Update is called once per frame
	void Update () {
	
		if(transform.parent.transform.parent.GetComponent<PlayerMovement>().shoot_btn == false)
		{
			need_keyup = false;
			if(need_cooldown == false)
			t = 0f;
		}
		
		if(t <= 0f)
		{
			transform.parent.transform.LookAt(target.transform.position);
			
			if(transform.parent.transform.parent.GetComponent<PlayerMovement>().shoot_btn == true && 
			   PlayerHealth.charge > 0f && need_keyup==false)
			{
				Vector3 r = new Vector3(Random.Range(-100,100),Random.Range(-100,100),0f).normalized*spread;
				transform.parent.transform.LookAt(target.transform.position+r);
				
				Vector3 dir = (target.transform.position+r) - transform.position;
				GameObject ob = Instantiate(bullet[gun_level-1],transform.position,transform.rotation)as GameObject;
				ob.rigidbody2D.velocity = transform.up * speed;
				
				LookAtZ(ob,target.transform.position);
				ob.transform.position+=ob.transform.up*r.x/5f;
				
				Destroy(ob,10f);
				
				t = rate;
				
				hp_system.ReduceCharge(cost);
				ob.GetComponent<Bullet>().shooter = "Player";
				
				if(automatic == false)
					need_keyup = true;
			}
		}
		else
			t -= Time.deltaTime;
			
			
			
		if(Input.GetKey(KeyCode.Keypad1))
		{
			gun_level = 1;
			ApplyGunLevel();
		}
		if(Input.GetKey(KeyCode.Keypad2))
		{
			gun_level = 2;
			ApplyGunLevel();
		}
		if(Input.GetKey(KeyCode.Keypad3))
		{
			gun_level = 3;
			ApplyGunLevel();
		}
		if(Input.GetKey(KeyCode.Keypad4))
		{
			gun_level = 4;
			ApplyGunLevel();
		}
		if(Input.GetKey(KeyCode.Keypad5))
		{
			gun_level = 5;
			ApplyGunLevel();
		}
		if(Input.GetKey(KeyCode.Keypad6))
		{
			gun_level = 6;
			ApplyGunLevel();
		}
		if(Input.GetKey(KeyCode.Keypad7))
		{
			gun_level = 7;
			ApplyGunLevel();
		}
		if(Input.GetKey(KeyCode.Keypad8))
		{
			gun_level = 8;
			ApplyGunLevel();
		}
		if(Input.GetKey(KeyCode.Keypad9))
		{
			gun_level = 9;
			ApplyGunLevel();
		}
		if(Input.GetKey(KeyCode.Keypad0))
		{
			gun_level = 10;
			ApplyGunLevel();
		}
	}
}
